package com.jf3q.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jf3q.common.domain.EduSubject;
import com.jf3q.common.service.EduSubjectService;
import com.jf3q.common.mapper.EduSubjectMapper;
import com.jf3q.common.vo.SubjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 李天琪
* @description 针对表【edu_subject(课程科目)】的数据库操作Service实现
* @createDate 2024-06-06 13:51:31
*/
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject>
    implements EduSubjectService{


    @Autowired
    EduSubjectMapper subjectMapper;

    @Override
    public List<SubjectVo> getList() {
        List<SubjectVo>vos=new ArrayList<>();
        List<EduSubject>list=this.list();

        for (EduSubject subject : list) {
            SubjectVo subjectVo=new SubjectVo();
            BeanUtils.copyProperties(subject,subjectVo);
            vos.add(subjectVo);
        }

        List<SubjectVo>tree=new ArrayList<>();
        for (SubjectVo vo : vos) {
            if ("0".equals(vo.getParentId())){
                tree.add(findChildren(vo,vos));
            }
        }
        return tree;
    }

    private SubjectVo findChildren(SubjectVo vo, List<SubjectVo> vos) {
        vo.setChildren(new ArrayList<>());
        for (SubjectVo subjectVo : vos) {
            if (vo.getId().equals(subjectVo.getParentId())){
                vo.getChildren().add(subjectVo);
                findChildren(subjectVo,vos);
            }

        }
        return vo;
    }
}




